package com.neusoft.neuiotms.dcom.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.persistence.EntityNotFoundException;
import javax.transaction.Transactional;

import org.activiti.editor.language.json.converter.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.neuiotms.dcom.converter.MonthServiceSummaryConverter;
import com.neusoft.neuiotms.dcom.dto.MonthServiceSummaryDTO;
import com.neusoft.neuiotms.dcom.model.MonthServiceSummary;
import com.neusoft.neuiotms.dcom.model.MonthWeekly;
import com.neusoft.neuiotms.dcom.repository.MonthServiceSummaryRepository;
import com.neusoft.neuiotms.dcom.repository.MonthWeeklyRepository;
import com.neusoft.neuiotms.dcom.service.MonthServiceSummaryService;

@Service
public class MonthServiceSummaryServiceImpl implements MonthServiceSummaryService {

    @Autowired
    private MonthServiceSummaryConverter monthServiceSummaryConverter;
    @Autowired
    private MonthServiceSummaryRepository monthServiceSummaryRepository;
    @Autowired
    private MonthWeeklyRepository monthWeeklyRepository;

    @Override
    public List<MonthServiceSummaryDTO> getInfoByMonthId(final Long monthId) {
        final List<MonthServiceSummary> list = this.monthServiceSummaryRepository.findByMonthId(monthId);
        return this.monthServiceSummaryConverter.toListDTO(list);
    }

    @Transactional
    @Override
    public List<MonthServiceSummaryDTO> update(final Long monthId, final List<MonthServiceSummaryDTO> dtos) {

        MonthWeekly model;
        try {
            model = this.monthWeeklyRepository.getOne(monthId);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException("未找到对应的月报！");
        }

        final List<MonthServiceSummary> models = new ArrayList<>();

        this.monthServiceSummaryRepository.deleteByMonthId(monthId);

        final List<Integer> totalNumber = dtos.stream().map(l -> l.getTotalNumber()).collect(Collectors.toList());
        final List<Integer> totalTime = dtos.stream().map(l -> l.getTotalTime()).collect(Collectors.toList());

        model.setServiceTotalNumber(
                CollectionUtils.isNotEmpty(totalNumber) ? totalNumber.stream().reduce((i, j) -> i + j).get() : 0);

        model.setServiceTotalTime(
                CollectionUtils.isNotEmpty(totalTime) ? totalTime.stream().reduce((i, j) -> i + j).get() : 0);

        this.monthWeeklyRepository.save(model);

        dtos.stream().map(l -> {
            final MonthServiceSummary item = this.monthServiceSummaryConverter.toModel(l);
            item.setMonth(model);
            return item;
        }).forEach(models::add);

        if (models.size() > 0) {
            this.monthServiceSummaryRepository.saveAll(models);
        }

        return this.monthServiceSummaryConverter.toListDTO(models);
    }

}
